<template>
  <div class="header">
    <van-icon name="arrow-left" size="30" @click="router.go(-1)" />
    <van-icon name="success" size="30" @click="handleAdd()" />
  </div>
  <div class="content">
    <van-cell-group inset>
      <van-field
        v-model="detailData.content"
        rows="1"
        autosize
        type="textarea"
        maxlength="550"
        placeholder="请输入"
        show-word-limit
      />
    </van-cell-group>
  </div>
</template>
    
<script lang="ts">
import { showNotify } from "vant";
import { defineComponent, handleError, onMounted, ref, Ref, watch } from "vue";
import { getDetail, nodeAdd, todoAdd, nodeChange,todoChange, Add ,getTodoDetail,NodeData,NodeOrTodo } from "../api/index";
import { useRoute, useRouter } from "vue-router";
export default defineComponent({
  props: {
    clickIndex: {
      type: Number,
    },
  },
  setup(props) {
    onMounted(() => {
      if (id) {
        getDetails();
      }
    });
    const route = useRoute();
    const router = useRouter();
    const id = route.query._id;
    const clickIndex = route.query.clickIndex;
    const date = new Date();
    let detailData = ref({}as Add);
    // 查看详情
    const getDetails = async (): Promise<void> => {
      try {
        let api = getDetail
        let data = {} as Add
        if (clickIndex === "1") {
            data = (await getTodoDetail(id as string)).data[0] as Add;
            console.log(data,'data--------------------data');
        }else{
            data = (await getDetail(id as string)).data as Add;
        }
        detailData.value.content = data.content;
        if (data._id) {
          detailData.value._id = data._id;
        }
      } catch (e) {
        console.log(e);
      }
    };
    const goBack = () => {
      router.go(-1);
    };
    const handleAdd = async (): Promise<void> => {
      //新增
      if (!detailData.value._id) {
        let api = nodeAdd;
        let addData = {
          dates: `${date.getFullYear()}年${
            date.getMonth() + 1
          }月${date.getDate()}日` as string,
          content: detailData.value.content as string,
        } as Add;
        if (clickIndex === "1") {
          api = todoAdd
          addData={
            isdone: false,
            content: detailData.value.content as string,
          }as Add
        } 
        try {
          const { data } = (await api(addData)) as NodeOrTodo;
          showNotify({ type: "success", message: "新增成功" });
          setTimeout(goBack, 300);
        } catch (e) {
          console.log(e);
        }
      } else {
        //修改
        try {
          detailData.value.dates = `${date.getFullYear()}年${
            date.getMonth() + 1
          }月${date.getDate()}日` as string;
          const { data } = (await nodeChange(
            detailData.value,
            detailData.value._id
          )) as NodeOrTodo;
          showNotify({ type: "success", message: "修改成功" });
          setTimeout(goBack, 300);
        } catch (e) {
          console.log(e);
        }
      }
    };
    return {
      router,
      detailData,
      handleAdd,
    };
  },
});
</script>
<style lang="scss" scoped>
.header {
  padding: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
    